#include "timer.h"






#ifdef HAS_TIMER0

static void (*timer0_callback)() = 0;

#if !((SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H))
static unsigned int timer0_reload_tick;
#endif
	
void timer0_int (void) interrupt INTERRUPT_VECTOR_TIMER0
{
#if !((SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H))
	TH0 = timer0_reload_tick>>8;
	TL0 = timer0_reload_tick&0xff;
#endif	
	if(timer0_callback)	timer0_callback();
}

void timer0_init(unsigned int tick, void callback(void))
{
	TIMER0_SET_USE_INTERNAL_SIGNAL();
	TIMER0_GATE_DISABLE();
	
#if (SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H)
	TIMER0_SET_16BIT_AUTO_RELOAD_MODE();
#else
	TIMER0_SET_16BIT_MANUAL_RELOAD_MODE();
	timer0_reload_tick = tick;
#endif
	
	TH0 = tick>>8;
	TL0 = tick&0xff;
	TF0 = 0;
	ET0 = 1;
	TR0 = 0;
	timer0_callback = callback;
}

void timer0_start()
{
	TR0 = 1;
}

void timer0_stop()
{
	TR0 = 0;
}

#endif


#ifdef HAS_TIMER1

static void (*timer1_callback)() = 0;

#if !((SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H))
static unsigned int timer1_reload_tick;
#endif

void timer1_int (void) interrupt INTERRUPT_VECTOR_TIMER1 
{
#if !((SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H))
	TH1 = timer1_reload_tick>>8;
	TL1 = timer1_reload_tick&0xff;
#endif	
	if(timer1_callback)	timer1_callback();
}


void timer1_init(unsigned int tick, void callback(void))
{
#if (SERIES == SERIES_STC8A) || (SERIES == SERIES_STC8G) || (SERIES == SERIES_STC8H)
	TIMER1_SET_16BIT_AUTO_RELOAD_MODE();
#else
	TIMER1_SET_16BIT_MANUAL_RELOAD_MODE();
	timer1_reload_tick = tick;
#endif
	TH1 = tick>>8;
	TL1 = tick&0xff;
	TF1 = 0;
	ET1 = 1;
	TR1 = 0;
	timer1_callback = callback;
}	

void timer1_start()
{
	TR1 = 1;
}

void timer1_stop()
{
	TR1 = 0;
}
#endif



#ifdef HAS_TIMER2

static void (*timer2_callback)() = 0;

void timer2_int (void) interrupt INTERRUPT_VECTOR_TIMER2
{
	TIMER2_INTERRUPT_FLAG_CLEAR();
	if(timer2_callback)	timer2_callback();
}

void timer2_init(unsigned int tick, void callback(void))
{
	TH2 = tick>>8;
	TL2 = tick&0xff;

#if defined(N76E003)
	TIMER2_RELOAD_ENABLE();
	TIMER2_SET_RELOAD_VALUE(tick);
#endif	
	
	TIMER2_INTERRUPT_FLAG_CLEAR();
	TIMER2_TRIGGER_DISABLE();
	TIMER2_INTERRUPT_ENABLE();
	timer2_callback = callback;
}

void timer2_start()
{
	TIMER2_TRIGGER_ENABLE();
}

void timer2_stop()
{
	TIMER2_TRIGGER_DISABLE();
}
#endif


#ifdef HAS_TIMER3
											
static void (*timer3_callback)() = 0;


void timer3_int (void) interrupt INTERRUPT_VECTOR_TIMER3
{
	if(timer3_callback)	timer3_callback();
}

void timer3_init(unsigned int tick, void callback())
{
	TH3 = tick>>8;
	TL3 = tick&0xff;		
	TIMER3_INTERRUPT_FLAG_CLEAR();	
	TIMER3_TRIGGER_DISABLE();
	TIMER3_INTERRUPT_ENABLE();
	timer3_callback = callback;
}

void timer3_start()
{
	TIMER3_TRIGGER_ENABLE();
}

void timer3_stop()
{
	TIMER3_TRIGGER_DISABLE();
}
#endif


#ifdef HAS_TIMER4
							
static void (*timer4_callback)() = 0;

void timer4_int (void) interrupt INTERRUPT_VECTOR_TIMER4
{
	if(timer4_callback)	timer4_callback();	
}


void timer4_init(unsigned int tick, void callback(void))
{
	TH4 = tick>>8;
	TL4 = tick&0xff;
	TIMER4_INTERRUPT_FLAG_CLEAR();
	TIMER4_TRIGGER_DISABLE();
	TIMER4_INTERRUPT_ENABLE();
	timer4_callback = callback;
}	

void timer4_start()
{
	TIMER4_TRIGGER_ENABLE();
}

void timer4_stop()
{
	TIMER4_TRIGGER_DISABLE();
}
#endif











